﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DTOService;
using System.Data;

namespace DAOService
{
    public class DAOBangDia : IDAOBangDia
    {
        Provider p = new Provider();

        #region 1041131
        public string SetMaBangDia()
        {
            string ma = "";
            try
            {
                DataTable dt = new DataTable();
                dt = p.ExecuteStoreProcedure("sp_DanhSachMaBangDia");
                BangDia bd = new BangDia();
                if (dt.Rows.Count != 0)
                {
                    int k = 1;
                    int i=0;
                    while (i < dt.Rows.Count)
                    {
                        
                        //sai
                        string s1 = dt.Rows[i][0].ToString();
                        int mabd = int.Parse(s1.Substring(2,8).ToString());
                        if (k == mabd)
                        {
                            k++;
                            i++;
                        }
                        else { break; }
                    }                        
                    //BD00000003
                    ma = k.ToString();
                    while(ma.ToString().Length < 8)
                    {
                        ma = '0' + ma;
                    }
                    ma = "BD" + ma;
                }
                else
                {
                    ma = "BD00000001";
                }                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return ma;
        }
        public Object ConvertOneRowIntoADTO(DataTable dt, int i)
        {
            BangDia temp = new BangDia();
            temp.MaBD = dt.Rows[i][0].ToString();
            temp.TenBD = dt.Rows[i][1].ToString();
            temp.LoaiBD = int.Parse(dt.Rows[i][2].ToString());
            temp.LoaiSanPham = int.Parse(dt.Rows[i][3].ToString());
            temp.GiaBD = float.Parse(dt.Rows[i][4].ToString());
            temp.SoLuong = int.Parse(dt.Rows[i][5].ToString());
            temp.TinhTrang = int.Parse(dt.Rows[i][6].ToString());
            temp.MoTa = dt.Rows[i][7].ToString();
            return temp;
        }
        #endregion

        public BangDia[] SelectBangDia()
        {
            BangDia[] rs;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachBangDia");
                rs = new BangDia[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    rs[i] = (BangDia)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return rs;            
        }

        public int UpdateBangDia(BangDia bd)
        {
            int kq = 0;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_CapNhatBangDia", new string[] { "@mabd", "@tenbangdia", "@loaibd", "@loaisanpham", "@giabd", "@soluong", "@tinhtrang", "@mota" }, new object[] { bd.MaBD, bd.TenBD, bd.LoaiBD, bd.LoaiSanPham, bd.GiaBD, bd.SoLuong, bd.TinhTrang, bd.MoTa });
                kq = int.Parse(dt.Rows[0]["STATUS"].ToString());
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return kq;            
        }

        public int DeleteBangDia(String mabd)
        {
            int kq = 0;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_XoaBangDia", new string[] { "@mabd" }, new object[] { mabd });
                kq = int.Parse(dt.Rows[0]["STATUS"].ToString());
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return kq;            
        }

        public bool InsertBangDia(BangDia bd)
        {            
            bool kq = false;
            try
            {
                bd.MaBD=SetMaBangDia();
                DataTable dt = p.ExecuteStoreProcedure("sp_ThemBangDia", new string[] { "@mabd", "@tenbangdia", "@loaibd", "@loaisanpham", "@giabd", "@soluong", "@tinhtrang", "@mota" }, new object[] { bd.MaBD, bd.TenBD, bd.LoaiBD, bd.LoaiSanPham, bd.GiaBD, bd.SoLuong, bd.TinhTrang, bd.MoTa });
                if (dt.Rows[0][0].ToString() == "1")
                    kq = true;                
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return kq;
        }

        public BangDia GetBangDia(string ma)
        {
            BangDia temp = new BangDia();
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_GetBangDia", new string[] { "@ma" }, new object[] { ma });
                if (dt.Rows.Count != 0)
                {
                    temp = (BangDia)ConvertOneRowIntoADTO(dt, 0);
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return temp;
        }

        public int GhiNhanTinhTrang(string ma, int flag)
        {
            int kq = 0;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_GhiNhanTinhTrang", new string[] { "@ma", "@flag" }, new object[] { ma, flag });
                kq = int.Parse(dt.Rows[0]["STATUS"].ToString());
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return kq;  
        }

        public BangDia[] SelectBangDia(string name)
        {
            BangDia[] rs;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachBangDiaTheoTen", new string[] { "@name" }, new object[] { name });
                rs = new BangDia[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    rs[i] = (BangDia)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return rs;
        }

        public BangDia[] TimKiemBangDia(BangDia bd, int pagesize, int skip)
        {
            BangDia[] rs;
            try
            {
                DataTable dt = new DataTable();
                dt = p.ExecuteStoreProcedure("sp_AdvancedSearch", new string[] { "@pagesize", "@skip", "@tenbd", "@loaisanpham", "@loaibd", "@tinhtrang" }, new object[] { pagesize, skip, bd.TenBD, bd.LoaiSanPham, bd.LoaiBD, bd.TinhTrang });
                rs = new BangDia[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    rs[i] = (BangDia)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return rs;
        }

        public int NumberBangDia()
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_NumberBangDia");
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public int NumberBangDia(BangDia bd)
        {
            int totalRecords = 0;
            DataTable dtCount = new DataTable();
            dtCount = p.ExecuteStoreProcedure("sp_NumberBangDiaSearch", new string[] { "@tenbd", "@loaisanpham", "@loaibd", "@tinhtrang" }, new object[] { bd.TenBD, bd.LoaiSanPham, bd.LoaiBD, bd.TinhTrang });            
            if (dtCount.Rows.Count != 0)
                totalRecords = int.Parse(dtCount.Rows[0][0].ToString());
            return totalRecords;                   
        }

        public BangDia[] SelectBangDia_Limit(int pagesize, int skip)
        {
            BangDia[] rs;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_ShowLimit_BangDia", new string[] { "@pagesize", "@skip" }, new object[] { pagesize, skip });
                rs = new BangDia[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    rs[i] = (BangDia)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return rs; 
        }

        public BangDia[] SelectBangDiaThueNhieuNhat()
        {
            BangDia[] rs;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_BangDiaThueNhieuNhat");
                rs = new BangDia[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    rs[i] = (BangDia)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return rs;
        }

        public BangDia[] ThongKeBangDiaThueNhieu(int thang, int nam)
        {
            BangDia[] arr;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_ThongKeBangDiaThueNhieu", new string[] { "@thang", "@nam" }, new object[] { thang, nam });
                arr = new BangDia[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {                    
                    BangDia temp = new BangDia();
                    temp.MaBD = dt.Rows[i][0].ToString();
                    temp.TenBD = dt.Rows[i][1].ToString();                    
                    temp.SoLuong = int.Parse(dt.Rows[i][2].ToString());
                    arr[i] = temp;
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return arr;
        }
    }
}
